<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
const tsKeyId = generateTestEntityId(primaryKeyType);
_%>
/* tslint:disable max-line-length */
import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils';
import sinon, { SinonStubbedInstance } from 'sinon';

import * as config from '@/shared/config/config';
import <%= entityAngularName %>Component from '@/entities/<%= entityFolderName %>/<%= entityFileName %>.vue';
import <%= entityAngularName %>Class from '@/entities/<%= entityFolderName %>/<%= entityFileName %>.component';
import <%= entityAngularName %>Service from '@/entities/<%= entityFolderName %>/<%= entityFileName %>.service';

const localVue = createLocalVue();

config.initVueApp(localVue);
<%_ if (enableTranslation) { _%>
const i18n = config.initI18N(localVue);
<%_ } _%>
const store = config.initVueXStore(localVue);
localVue.component('font-awesome-icon', {});
localVue.component('b-badge', {});
<%_ if (pagination !== 'no') { _%>
localVue.component('jhi-sort-indicator', {});
<%_ } _%>
localVue.directive('b-modal', {});
localVue.component('b-button', {});
localVue.component('router-link', {});

const bModalStub = {
  render: () => {},
  methods: {
    hide: () => {},
    show: () => {}
  }
};

describe('Component Tests', () => {
  describe('<%= entityAngularName %> Management Component', () => {
    let wrapper: Wrapper<<%= entityAngularName %>Class>;
    let comp: <%= entityAngularName %>Class;
    let <%= entityInstance %>ServiceStub: SinonStubbedInstance<<%= entityAngularName %>Service>;

    beforeEach(() => {
      <%= entityInstance %>ServiceStub = sinon.createStubInstance<<%= entityAngularName %>Service>(<%= entityAngularName %>Service);
      <%= entityInstance %>ServiceStub.retrieve.resolves({ headers: {} });

      wrapper = shallowMount<<%= entityAngularName %>Class>(<%= entityAngularName %>Component, {
        store,
        <%_ if (enableTranslation) { _%>
        i18n,
        <%_ } _%>
        localVue,
        stubs: {<%_ if (pagination !== 'no') { _%>jhiItemCount:true, bPagination:true,<%_ } _%> bModal: bModalStub as any},
        provide: {
          <%= entityInstance %>Service: () => <%= entityInstance %>ServiceStub
        }
      });
      comp = wrapper.vm;
    });

    it('Should call load all on init', async () => {
      // GIVEN
      <%= entityInstance %>ServiceStub.retrieve.resolves({ headers: {}, data: [{ id: <%- tsKeyId %> }] });

      // WHEN
      comp.retrieveAll<%= entityAngularName %>s();
      await comp.$nextTick();

      // THEN
      expect(<%= entityInstance %>ServiceStub.retrieve.called).toBeTruthy();
      expect(comp.<%= entityInstancePlural %>[0]).toEqual(jasmine.objectContaining({id: <%- tsKeyId %>}));
    });
<%_ if (pagination !== 'no') { _%>

    it('should load a page', async () => {
      // GIVEN
      <%= entityInstance %>ServiceStub.retrieve.resolves({ headers: {}, data: [{ id: <%- tsKeyId %> }] });
      comp.previousPage = 1;

      // WHEN
      comp.loadPage(2);
      await comp.$nextTick();

      // THEN
      expect(<%= entityInstance %>ServiceStub.retrieve.called).toBeTruthy();
      expect(comp.<%= entityInstancePlural %>[0]).toEqual(jasmine.objectContaining({id: <%- tsKeyId %>}));
    });
    <%_ if (pagination !== 'infinite-scroll') { _%>

    it('should not load a page if the page is the same as the previous page', () => {
      // GIVEN
      <%= entityInstance %>ServiceStub.retrieve.reset();
      comp.previousPage = 1;

      // WHEN
      comp.loadPage(1);

      // THEN
      expect(<%= entityInstance %>ServiceStub.retrieve.called).toBeFalsy();
    });
    <%_ } _%>

    it('should re-initialize the page', async () => {
      // GIVEN
      <%= entityInstance %>ServiceStub.retrieve.reset();
      <%= entityInstance %>ServiceStub.retrieve.resolves({ headers: {}, data: [{ id: <%- tsKeyId %> }] });

      // WHEN
      comp.loadPage(2);
      await comp.$nextTick();
      comp.clear();
      await comp.$nextTick();

      // THEN
      expect(<%= entityInstance %>ServiceStub.retrieve.callCount).toEqual(<%_ if (pagination === 'pagination' || pagination === 'pager' ) { _%>3<%_ } else { _%>2<%_ } _%>);
      expect(comp.page).toEqual(1);
      expect(comp.<%= entityInstancePlural %>[0]).toEqual(jasmine.objectContaining({id: <%- tsKeyId %>}));
    });
    <%_ if (pagination === 'infinite-scroll') { _%>

    it('should calculate the sort attribute for an id', () => {
      // WHEN
      const result = comp.sort();

      // THEN
      expect(result).toEqual(['id,asc']);
    });

    it('should calculate the sort attribute for a non-id attribute', () => {
      // GIVEN
      comp.propOrder = 'name';

      // WHEN
      const result = comp.sort();

      // THEN
      expect(result).toEqual(['name,asc', 'id']);
    });
    <%_ } else { _%>

    it('should calculate the sort attribute for an id', () => {
      // WHEN
      const result = comp.sort();

      // THEN
      expect(result).toEqual(['id,asc']);
    });

    it('should calculate the sort attribute for a non-id attribute', () => {
      // GIVEN
      comp.propOrder = 'name';

      // WHEN
      const result = comp.sort();

      // THEN
      expect(result).toEqual(['name,asc', 'id']);
    });
    <%_ } _%>
<%_ } _%>
<%_ if (!readOnly) { _%>
    it('Should call delete service on confirmDelete', async () => {
      // GIVEN
      <%= entityInstance %>ServiceStub.delete.resolves({});

      // WHEN
      comp.prepareRemove({id: <%- tsKeyId %>});
      comp.remove<%= entityAngularName %>();
      await comp.$nextTick();

      // THEN
      expect(<%= entityInstance %>ServiceStub.delete.called).toBeTruthy();
      expect(<%= entityInstance %>ServiceStub.retrieve.callCount).toEqual(1);
    });
<%_ } _%>
  });
});
